package org.ala.tcp_feign_server.handler;

import org.ala.tcp_feign_common.handler.ByteMessageReadHandler;
import org.ala.tcp_feign_common.handler.MessageDecodeHandler;
import org.ala.tcp_feign_common.handler.MessageEncodeHandler;
import org.ala.tcp_feign_common.logs.LoggerFactory;
import org.ala.tcp_feign_server.context.TcpFeignServerContext;
import org.ala.tcp_feign_server.handler.biz.BizByteMessageAction;
import org.ala.tcp_feign_server.handler.heart.HeartByteMessageAction;
import org.ala.tcp_feign_server.handler.login.LoginByteMessageAction;
import org.slf4j.Logger;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.SocketChannel;


/**
 * 初始化handler
 * 
 * 物理解码 -> 业务解码 -> 业务分发 -> 业务编码 -> 物理编码
 *
 * @author 骆毅(ala)
 * @date 2024年4月19日
 */
public class TcpFeignServerInitializeHandler extends ChannelInitializer<SocketChannel> {
	
	
	static Logger log = LoggerFactory.getLogger("tcp-feign", "server");
	

	@Override
	protected void initChannel(SocketChannel ch) throws Exception {
		log.info("[TcpFeignServerInitializeHandler initChannel] 收到新的客户端连接 remoteAddress:" + ch.remoteAddress());
		
		//	物理解码 -> 消息action -> 物理编码
		ch.pipeline().addLast(new MessageDecodeHandler());
		ch.pipeline().addLast(actionHandler());
		ch.pipeline().addLast(new MessageEncodeHandler());
		
		TcpFeignServerContext.instance().register(ch);
	}
	/**
	 * action
	 */
	protected ByteMessageReadHandler actionHandler() {
		ByteMessageReadHandler handler = new ByteMessageReadHandler();
		
		handler.append(new LoginByteMessageAction());
		handler.append(new HeartByteMessageAction());
		handler.append(new BizByteMessageAction());
		
		return handler;
	}

}
